package linkedlist;

/**
 * @author zhaoziqiang
 * @Description:
 * @date 2020/8/31 15:33
 */
public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }

    public ListNode(int[] arr){
        if(arr==null||arr.length==0)
            throw new IllegalArgumentException("arr cannot be empty");
        //当前ListNode为头结点
        this.val=arr[0];
        ListNode cur=this;
        for (int i = 1; i < arr.length; i++) {
            cur.next=new ListNode(arr[i]);
            cur=cur.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("head ");
        ListNode cur=this;
        while (cur!=null){
            sb.append(cur.val);
            sb.append("->");
            cur=cur.next;
        }
        sb.append("null tail");
        return sb.toString();
    }

}
